{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d547f7cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import pymysql\n",
    "\n",
    "MYSQL_CFG = {}\n",
    "\n",
    "with open(\"E:\\\\projs\\\\my\\\\mysqlcfg.json\",'r') as f:\n",
    "    cfg = json.load(f)\n",
    "    MYSQL_CFG = cfg\n",
    "\n",
    "conn = pymysql.connect(host=MYSQL_CFG['host'], user=MYSQL_CFG['username'], password=MYSQL_CFG['passwd'], database='demo')\n",
    "# cursor = conn.cursor()  # 执行完毕返回的结果集默认以元组显示\n",
    "# 得到一个可以执行SQL语句并且将结果作为字典返回的游标\n",
    "cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)\n",
    "\n",
    "# 定义要执行的SQL语句\n",
    "sql = \"\"\"\n",
    "CREATE TABLE t_user_03 (\n",
    "id INT auto_increment PRIMARY KEY ,\n",
    "name CHAR(10) NOT NULL,\n",
    "age TINYINT NOT NULL\n",
    ")ENGINE=innodb DEFAULT CHARSET=utf8;  #注意：charset='utf8' 不能写成utf-8\n",
    "\"\"\"\n",
    "# 执行SQL语句\n",
    "cursor.execute(sql)\n",
    " \n",
    "# 关闭光标对象\n",
    "cursor.close()\n",
    "conn.close()\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae2b23ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import pymysql\n",
    "\n",
    "MYSQL_CFG = {}\n",
    "\n",
    "with open(\"E:\\\\projs\\\\my\\\\mysqlcfg.json\",'r') as f:\n",
    "    cfg = json.load(f)\n",
    "    MYSQL_CFG = cfg\n",
    "\n",
    "conn = pymysql.connect(host=MYSQL_CFG['host'], user=MYSQL_CFG['username'], password=MYSQL_CFG['passwd'], database='demo')\n",
    "cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)\n",
    "\n",
    "sql='select * from t_users '\n",
    "print(sql)\n",
    "res=cursor.execute(sql)\n",
    "print(res)\n",
    " \n",
    "# 关闭光标对象\n",
    "cursor.close()\n",
    "conn.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3e2a01c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import pymysql\n",
    "\n",
    "MYSQL_CFG = {}\n",
    "\n",
    "with open(\"E:\\\\projs\\\\my\\\\mysqlcfg.json\",'r') as f:\n",
    "    cfg = json.load(f)\n",
    "    MYSQL_CFG = cfg\n",
    "\n",
    "conn = pymysql.connect(host=MYSQL_CFG['host'], user=MYSQL_CFG['username'], password=MYSQL_CFG['passwd'], database='demo')\n",
    "cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)\n",
    "\n",
    "# 插入单条记录\n",
    "sql ='insert into t_user_03 (name,age) values (%s,%s);'\n",
    "name = 'wuli'\n",
    "age= 10\n",
    "cursor.execute(sql, [name, age])\n",
    "last_id = cursor.lastrowid\n",
    "print(\"最后一条数据的ID是:\", last_id)\n",
    "\n",
    "# sql = 'insert into t_user_03(name,age) values(%s,%s);'\n",
    "# data = [\n",
    "#     ('july1', 14),\n",
    "#     ('june2', 25),\n",
    "#     ('marin3', 34)\n",
    "# ]\n",
    "# # 拼接并执行sql语句\n",
    "# res = cursor.executemany(sql, data)\n",
    "\n",
    "conn.commit()\n",
    " \n",
    "# 关闭光标对象\n",
    "cursor.close()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e004d73b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import pymysql\n",
    "\n",
    "MYSQL_CFG = {}\n",
    "\n",
    "with open(\"E:\\\\projs\\\\my\\\\mysqlcfg.json\",'r') as f:\n",
    "    cfg = json.load(f)\n",
    "    MYSQL_CFG = cfg\n",
    "\n",
    "conn = pymysql.connect(host=MYSQL_CFG['host'], user=MYSQL_CFG['username'], password=MYSQL_CFG['passwd'], database='demo')\n",
    "cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)\n",
    "\n",
    "sql = 'delete from t_user_03 where id=%s'\n",
    "rows = cursor.execute(sql, 1)\n",
    "conn.commit()\n",
    "# 关闭光标对象\n",
    "cursor.close()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce13a8a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import pymysql\n",
    "\n",
    "MYSQL_CFG = {}\n",
    "\n",
    "with open(\"E:\\\\projs\\\\my\\\\mysqlcfg.json\",'r') as f:\n",
    "    cfg = json.load(f)\n",
    "    MYSQL_CFG = cfg\n",
    "\n",
    "conn = pymysql.connect(host=MYSQL_CFG['host'], user=MYSQL_CFG['username'], password=MYSQL_CFG['passwd'], database='demo')\n",
    "cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)\n",
    "\n",
    "sql = \"update t_user_03 set name=%s where id=%s\"\n",
    "rows = cursor.executemany(sql, [('李磊11', 2)])\n",
    "print(rows)\n",
    "conn.commit()\n",
    "# 关闭光标对象\n",
    "cursor.close()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a6b4f3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import pymysql\n",
    "\n",
    "MYSQL_CFG = {}\n",
    "\n",
    "with open(\"E:\\\\projs\\\\my\\\\mysqlcfg.json\",'r') as f:\n",
    "    cfg = json.load(f)\n",
    "    MYSQL_CFG = cfg\n",
    "\n",
    "conn = pymysql.connect(host=MYSQL_CFG['host'], user=MYSQL_CFG['username'], password=MYSQL_CFG['passwd'], database='demo')\n",
    "cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)\n",
    "\n",
    "sql = 'select id,name from t_user_03 where id = %s'\n",
    "row = cursor.execute(sql, 2)\n",
    "result = cursor.fetchall() # 打印出 [{'id': 2, 'name': '李磊11'}]\n",
    "print(result)\n",
    " \n",
    "# 关闭光标对象\n",
    "cursor.close()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01b6cdd3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
